<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="企业名称">
            <a-input v-decorator="['name']" placeholder="请输入企业名称" />
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
        </a-form>
      </a-row>

      <!-- 操作板块 -->
      <div class="row-item-tab clearfix">

        <a-button
          class="fl-l"
          type="primary"
          icon="plus"
          @click="handleEdit(null)"
        >创建微信企业</a-button>
      </div>
    </div>
    <s-table
      ref="table"
      rowKey="id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    >
      <!-- 操作 -->
      <span class="actions" slot="action" slot-scope="text,item">
        <a v-action:edit @click="handleEdit(item)">编辑</a>
        <a style='color: red;' v-action:delete @click="handleDelete(item.id)">删除</a>
      </span>
    </s-table>
    <ItemForm ref="Form" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Api from '@/api/firmWx'
import { STable } from '@/components'
import ItemForm from './modules/ItemForm'
import adActionType from '@/common/enum/adActionType'
export default {
  name: 'Index',
  components: {
    STable,
    ItemForm
  },
  data () {
    return {
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      adActionType,
      // 表头
      columns: [
        {
          title: '企业ID',
          dataIndex: 'corp_id'
        },
        {
          title: '企业名称',
          dataIndex: 'name'
        },
        {
          title: '应用秘钥',
          dataIndex: 'secret'
        },
        {
          title: '应用Token',
          dataIndex: 'token'
        },
        {
          title: 'aes_key',
          dataIndex: 'aes_key'
        },
        {
          title: '创建时间',
          dataIndex: 'create_time'
        },
        {
          title:'操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => response.data.list)
      },
    }
  },
  created () {
  },
  methods: {
    /**
     * 删除记录
     */
    handleDelete (id) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk () {
          return Api.del({id})
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => modal.destroy())
        }
      })
    },

    // 确认搜索
    handleSearch (e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    },
    // 修改状态()
    handleUpdateStatus (id, state) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要'+(state===10?'停用':'启用')+'该记录吗?',
        onOk () {
          // return Api.changeStatus({id})
          //   .then(result => {
          //     // 显示成功
          //     app.$message.success(result.message, 1.5)
          //     app.handleRefresh()
          //   })
          //   .finally(result => modal.destroy())
        }
      })

    },
    handleEdit(item) {
      console.log(2222, item);
      // item.image_url = "http://admintest.whbjswh.com/uploads/10001/20240228/b93625ff3b8485db41e392befdb8c56b.png"
      // item.image_id = item.image_url || ''
      this.$refs.Form.edit(item)
    }

  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
</style>
